#include "sys.h"

bit flag500ms;
bit SendFlag;
u8 SetTime = 60;
char WorkTime; 
char Temp;
u8 PWM;
void main(void)
{
	all_init();
	Timer0Init();
	Timer1Init();
	WorkTime = SetTime;
	while(1)
	{
		if(flag500ms)
		{
			if(Start_Ds18B20())
			{
				Temp = Read_Temp();
			}
		}
		if(SendFlag && (WorkTime!=0))
		{
			DA_Pcf8591(0xff);
		}else{
			DA_Pcf8591(0x00);
		}
		show();
		work();
		key_action();
	}
}

void work()
{
	if(WorkTime)
	{
		switch(showMode)
		{
			case 1: PWM = 20;led_illume(0xfe);break;
			case 2: PWM = 30;led_illume(0xfd);break;
			case 3: PWM = 70;led_illume(0xfb);break;
		}
	}else
	{
		PWM  = 0;
		led_illume(0xff);
	}
}
void timer0() interrupt 1
{
	static u16 T0_count1;
	static u16 T0_count2;
	T0_count2++;
	T0_count1++;
	if(T0_count1 == 500)
	{
		T0_count1 = 0;
		if(SetTime != 0)
		{
			WorkTime--;
			if(WorkTime < 0)
			{
				WorkTime = SetTime;
				showMode++;
				if(showMode == 4)
				{
					showMode = 1;
				}
			}
		}
	}
	if(T0_count2 == 500)
	{
		T0_count2 = 0;
		flag500ms = ~flag500ms;
	}
	nixie_show();
	nixie_scan();
}

void timer1() interrupt 3
{
	static u8 T1_count1;

	T1_count1++;
	if(T1_count1 < PWM)
	{
		SendFlag = 1;
	}else{
		SendFlag = 0;
	}
	if(T1_count1 == 100)
	{
		T1_count1 = 0;
	}
	
}